import 'package:flutter/material.dart';
import 'package:test01/animations/FadeAnimation.dart';
import 'package:flutter/material.dart';
import 'package:page_transition/page_transition.dart';
import 'package:test01/pages/HomePage.dart';

class StarterPage extends StatefulWidget {
  @override
  _StarterPageState createState() => _StarterPageState();
}

class _StarterPageState extends State<StarterPage>
    with TickerProviderStateMixin {
  late AnimationController animationController;
  late Animation<double> animation;

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    animationController = AnimationController(
      vsync: this,
      duration: const Duration(
        milliseconds: 600,
      ),
    );
    animation = Tween<double>(
      begin: 1.0,
      end: 35.0,
    ).animate(animationController);
  }

  @override
  void dispose() {
    animationController.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Container(
        decoration: const BoxDecoration(
          color: Colors.black,
          image: DecorationImage(
            image: AssetImage('assets/images/starter-image.jpg'),
            fit: BoxFit.cover,
          ),
        ),
        child: Container(
          decoration: BoxDecoration(
            gradient: LinearGradient(begin: Alignment.bottomCenter, colors: [
              Colors.black.withOpacity(0.9),
              Colors.black.withOpacity(0.8),
              Colors.black.withOpacity(0.2),
            ]),
          ),
          child: Padding(
            padding: const EdgeInsets.all(22.0),
            child: buildColumn(),
          ),
        ),
      ),
    );
  }

  Column buildColumn() {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      mainAxisAlignment: MainAxisAlignment.end,
      children: [
        FadeAnimation(
          delay: 0.5,
          child: const Text(
            "快速在线下单",
            style: TextStyle(
                color: Colors.white, fontSize: 40, fontWeight: FontWeight.bold),
          ),
        ),
        const SizedBox(
          height: 20,
        ),
        FadeAnimation(
          delay: 1.0,
          child: const Text(
            "根据您的位置 \n 来查看您附近的美食",
            style: TextStyle(color: Colors.white, height: 1.4, fontSize: 18),
          ),
        ),
        const SizedBox(
          height: 100,
        ),
        FadeAnimation(
          delay: 1.2,
          child: ScaleTransition(
            scale: animation,
            child: Container(
              width: double.infinity,
              decoration: BoxDecoration(
                borderRadius: BorderRadius.circular(10),
                gradient:
                    LinearGradient(colors: [Colors.orange, Colors.yellow]),
              ),
              child: AnimatedOpacity(
                opacity: textVisible ? 1.0 : 0.0,
                duration: Duration(milliseconds: 50),
                child: MaterialButton(
                  child: Text("开始"),
                  onPressed: () {
                    goHomePageClick();
                  },
                ),
              ),
            ),
          ),
        ),
        const SizedBox(
          height: 33,
        ),
        FadeAnimation(
          delay: 1.4,
          child: AnimatedOpacity(
            opacity: textVisible ? 1.0 : 0.0,
            duration: const Duration(milliseconds: 50),
            child: const Center(
              child: Text(
                "Now Deliver To your Door 22/7",
                style: TextStyle(color: Colors.white, fontSize: 14),
              ),
            ),
          ),
        )
      ],
    );
  }

  bool textVisible = true;

  void goHomePageClick() async {
    setState(() {
      textVisible = false;
    });
    await animationController.forward();
    Navigator.push(
      context,
      PageTransition(
        child: HomePage(),
        type: PageTransitionType.fade,
      ),
    );
  }
}
